<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>货物详情</span>
          <div class="right-menu">
            <el-button type="primary" size="small" @click="handleEdit">编辑</el-button>
            <router-link :to="`/shipment/route/${shipmentId}`">
              <el-button type="success" size="small">查看路线</el-button>
            </router-link>
            <el-button size="small" @click="$router.back()">返回</el-button>
          </div>
        </div>
      </template>
      
      <div v-loading="loading">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="货物ID">{{ shipmentInfo.id }}</el-descriptions-item>
          <el-descriptions-item label="货物单号">{{ shipmentInfo.number_no }}</el-descriptions-item>
          <el-descriptions-item label="取货仓库">{{ shipmentInfo.pickup_warehouse }}</el-descriptions-item>
          <el-descriptions-item label="卸货仓库">{{ shipmentInfo.delivery_warehouse }}</el-descriptions-item>
          <el-descriptions-item label="设备号">{{ shipmentInfo.mac_id }}</el-descriptions-item>
          <el-descriptions-item label="版本号">{{ shipmentInfo.version }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ formatDateTime(shipmentInfo.create_time) }}</el-descriptions-item>
          <el-descriptions-item label="创建人">{{ shipmentInfo.create_by || '-' }}</el-descriptions-item>
          <el-descriptions-item label="更新时间" :span="2">{{ formatDateTime(shipmentInfo.update_time) }}</el-descriptions-item>
          <el-descriptions-item label="更新人" :span="2">{{ shipmentInfo.update_by || '-' }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </el-card>
    
    <!-- 编辑对话框 -->
    <el-dialog v-model="dialogVisible" title="编辑货物信息" width="500px" append-to-body>
      <el-form ref="shipmentFormRef" :model="shipmentForm" :rules="shipmentRules" label-width="100px">
        <el-form-item label="货物单号" prop="number_no">
          <el-input v-model="shipmentForm.number_no" placeholder="请输入货物单号" />
        </el-form-item>
        <el-form-item label="取货仓库" prop="pickup_warehouse">
          <el-input v-model="shipmentForm.pickup_warehouse" placeholder="请输入取货仓库" />
        </el-form-item>
        <el-form-item label="卸货仓库" prop="delivery_warehouse">
          <el-input v-model="shipmentForm.delivery_warehouse" placeholder="请输入卸货仓库" />
        </el-form-item>
        <el-form-item label="设备号" prop="mac_id">
          <el-input v-model="shipmentForm.mac_id" placeholder="请输入设备号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getShipment, updateShipment } from '@/api/shipment'

const route = useRoute()

const loading = ref(false)
const shipmentInfo = ref({})
const dialogVisible = ref(false)
const shipmentId = computed(() => route.params.id)

// 表单对象
const shipmentForm = ref({
  id: undefined,
  number_no: '',
  pickup_warehouse: '',
  delivery_warehouse: '',
  mac_id: '',
  version: 1
})

// 表单校验规则
const shipmentRules = {
  number_no: [{ required: true, message: '货物单号不能为空', trigger: 'blur' }],
  pickup_warehouse: [{ required: true, message: '取货仓库不能为空', trigger: 'blur' }],
  delivery_warehouse: [{ required: true, message: '卸货仓库不能为空', trigger: 'blur' }],
  mac_id: [{ required: true, message: '设备号不能为空', trigger: 'blur' }]
}

const shipmentFormRef = ref(null)

// 格式化日期时间
const formatDateTime = (dateTime) => {
  if (!dateTime) return '-'
  return new Date(dateTime).toLocaleString()
}

// 获取货物详情
const fetchShipmentInfo = async () => {
  loading.value = true
  try {
    const res = await getShipment(shipmentId.value)
    shipmentInfo.value = res.data || {}
  } catch (error) {
    console.error('获取货物详情失败:', error)
    ElMessage.error('获取货物详情失败')
  } finally {
    loading.value = false
  }
}

// 编辑货物
const handleEdit = () => {
  shipmentForm.value = {
    id: shipmentInfo.value.id,
    number_no: shipmentInfo.value.number_no,
    pickup_warehouse: shipmentInfo.value.pickup_warehouse,
    delivery_warehouse: shipmentInfo.value.delivery_warehouse,
    mac_id: shipmentInfo.value.mac_id,
    version: shipmentInfo.value.version
  }
  dialogVisible.value = true
}

// 提交表单
const submitForm = () => {
  shipmentFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        await updateShipment(shipmentForm.value.id, shipmentForm.value)
        ElMessage.success('修改成功')
        dialogVisible.value = false
        fetchShipmentInfo() // 刷新数据
      } catch (error) {
        console.error('保存货物信息失败:', error)
      }
    }
  })
}

onMounted(() => {
  fetchShipmentInfo()
})
</script>

<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.right-menu {
  display: flex;
  gap: 10px;
}
</style>